<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>发送邮件</title>
		<script type="text/javascript">
			//添加收件人邮箱
			function addUser(){
				var userId=window.showModalDialog("../addUser.jsp","",
					"dialogHeight:200px;dialogWidth:300px;dialogTop:250px;dialogLeft:400px;");
				if(userId!=false&&undefined!=userId){
					//得到userId文本框里的数据
					var idvalue=document.getElementById("userId").value;
					//判断发送的数量，一次最多只能给五个人发送
					var idarr=idvalue.split(";");
					//如果不存在此编号,则添加
					if(idarr.length>=6){
						alert("一次最多只可以给五个人同时发送！");
						return;
					}
					if(idvalue.indexOf(userId)==-1){
						//这里其实通过选择的用户编号查询到邮箱账号并且放到这里来！！！！
						document.getElementById("userId").value=idvalue+userId+";";
					}
				}
			}
			//删除收件人
			function deleteUser(){
				document.getElementById("divid").style.display="block";
				var tableData=document.getElementById("delData");
				//将收件人编号循环添加到表格中
				var idvalue=document.getElementById("userId").value;
				var idarr=idvalue.split(";");
				for(var i=0;i<idarr.length-1;i++){
					var trObj=document.createElement("tr");
					trObj.id="tr"+idarr[i];
					var td_id=document.createElement("td");
					td_id.innerHTML=idarr[i];
					var td_op=document.createElement("td");
					td_op.innerHTML="<a onclick='deleteUser2("+idarr[i]+")' style='cursor:pointer;'>删除</a>";
					trObj.appendChild(td_id);
					trObj.appendChild(td_op);
					tableData.appendChild(trObj);
				}
			}
			function deleteUser2(id){
				var idvalue=document.getElementById("userId").value;
				document.getElementById("userId").value=idvalue.replace(id+";","");
				var trObj=document.getElementById("tr"+id);
				document.getElementById("delData").removeChild(trObj);
			}
			//验证数据
			function checkData(){
				//判断上传的文件是否为空，为空的给默认值
				var file1=document.getElementById("file1").value;
				if(file1.length==0){
					document.getElementById("file1").value=null;
				}
				var file2=document.getElementById("file2").value;
				if(file2.length==0){
					document.getElementById("file2").value=null;
				}
				var file3=document.getElementById("file3").value;
				if(file3.length==0){
					document.getElementById("file3").value=null;
				}
				var userId=document.getElementById("userId").value;
				if(userId==""){
					alert("请选择收件人！");
					addUser();
					return false;
				}
				var topic=document.getElementById("topic").value;
				if(topic==""){
					alert("请填写email主题！");
					return false;
				}
				var emailText=document.getElementById("emailText").value;
				if(emailText==""){
					alert(	"请填写email正文！");
					return false;
				}
				return true;
			}
			//发送邮件
			function sendEmail(){
				if(checkData()){//验证没有成功
					document.getElementById("sendType").value="1";
					document.getElementById("formid").submit();
				}	
			}
			//保存邮件到草稿箱
			function saveEmail(){
				if(checkData()){
					document.getElementById("sendType").value="3";
					document.getElementById("formid").submit();
				}
			}
			//删除所有发件人
			function deleteAllUser(){
				document.getElementById("userId").value="";
			}
		</script>
		<style type="text/css">
			#divid{
				border: 2px solid red;
    			float: right;
    			height: 240px;
    			margin-left: 37px;
    			margin-top: -43px;
    			position: relative;
    			right: 158px;
    			top: 30px;
    			width: 240px;
    			display:none;
			}
			#sendId,#addressDiv{
				display:none;
			}
			body {
				background-image:
				url(${pageContext.request.contextPath}/images/forum7.jpg);
			}
		</style>
	</head>

	<body>
		<form method="post" action="${pageContext.request.contextPath}/sendEmail.action?method=saveEmail" id="formid" enctype="multipart/form-data"> 
		<div id="addressDiv">
				<input type="text" name="addressId" id="addressId" value="${sessionScope.login_User.userId }"/>
		</div>		
		<br><br>收件人: 
			<input type="text" name="userId" id="userId" readonly/>
			<input type="button" value="添加收件人" onclick="addUser();">
			<input type="button" value="删除收件人" onclick="deleteUser();">
			<input type="button" value="删除所有收件人" onclick="deleteAllUser();">
			<br/><br/>
			<div id="divid">
			      <center><font color="red" size=5px>请选择要删除的ID编号</font></center>
			      <table id="delData" align="center" border="1px">
			      
			      </table>
			</div>
			email主题：
			<input type="text" name="topic" id="topic"/><br/><br/>
			文件一选择：
			<input type="file" name="file1" id="file1" accept=".doc,.docx,.zip"/><br/><br/>
			文件二选择：
			<input type="file" name="file2" id="file2" accept=".doc,.docx,.zip"/><br/><br/>
			文件三选择：
			<input type="file" name="file3" id="file3" accept=".doc,.docx,.zip"/><br/><br/>
			<table border=0px>
				<tr>
					<td valign="middle">email正文：</td>
					<td><textarea rows="6" cols="50" id="emailText" name="emailText"></textarea><br/><br/></td>
				</tr>
			</table>
			<div id="sendId">
				<input type="text" name="sendType" id="sendType"/>
			</div>		
			<input type="button" value="发送" style="margin-left:200px" onclick="sendEmail();" id="send_Id">
			<input type="button" value="保存到草稿箱" onclick="saveEmail();" id="save_Id">
			<input type="reset" value="重置"> 
		</form>
	</body>
</html>
